<?php

namespace App\Http\Controllers\Home;
use Hash;
use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class UserController extends Controller
{
	
    //前台用户中心
    public function getIndex()
    {
    	$id=session('userid');
        $users = DB::table('user')->where('id',$id)->first();

    	 $user = DB::table('userdetail')->where('id',$id)->first();
         //dd($user);
        return view('Home.user.index',['user'=>$user],['users'=>$users]);
    }
    public function postDocenter(Request $request)
    {

        // dd($request->all());
        $aid = implode($request->only(['province','city','area','address']));
       //dd($aid);
        $id=$request->input('id');
        //dd($id);
         $data = $request->only(['username','sex','email','birth']);
         $data['aid']=$aid;
         $data['photo'] = $this->upload($request,'photo');
         //dd($data);
         //dd($id);
         // dd($data);
         $res = DB::table('userdetail') -> where('id',$id)->update($data);
         //dd($res);
          if($res){
            return redirect('/home/user/index')->with('success','修改成功');
        }else{
            // echo '失败';
            return back()->with('error','修改失败');
        }
         
    }
        //前台修改密码
     public function getPassword($id)
    {
          // dd($id); 
        
        $users = DB::table('user')->where('id',$id)->first();

         $user = DB::table('userdetail')->where('id',$id)->first();
         return view ('Home.user.password',['user'=>$user],['users'=>$users]);
      }
    //  //执行修改
     public function postPupdate(Request $request) 
     {
        //dd($request->all());
          $id=$request->input('id');
          $ps = $request->input('opassword');
        // dd($id);
         // $data = $request->only(['password']);
          //密码加密
          $data['password'] = Hash::make($request->input('password'));
           // dd($data);
          //查询数据进行比对
          $res = DB::table('user')->where('id',$id)->first();
          //dd($res);
        
        if(Hash::check($ps,$res->password))
        {
            
            if($request->input('password')==$request->input('rnpassword')){
                    $ress = DB::table('user')->where('id',$id)->update($data);
                   // dd($ress);
                return redirect('/home/user/index')->with('success','修改密码成功');
                }else{
                    // echo '失败';
                    return back()->with('error','修改密码失败');
                }
        }else{
              return back()->with('error','密码错误请重试');
        }
     }

     // public function postDophone(Request $request)
     // {
     //    $data['photo'] = $this->upload($request,'photo');

     //    dd($data);
     // }
     /*
        文件上传处理的方法
    */
    public function upload($request,$name)
    {
         //检测是否有文件上传
        if($request->hasFile($name)){
            //随机文件名
            $n = md5(rand(1,99999).time());
            $suffix = $request->file($name)->getClientOriginalExtension();
            $arr = array('jpg','png','jpeg');
            if(!in_array($suffix,$arr)){
                // return back();
                die('上传文件不符合要求');
            }
            // 参数1 移动的目标目录,参数2,文件名字
            $request->file($name)->move('./bootcss/home/Images/user',$n.'.'.$suffix);
            //需要在上传成功后返回一个文件的路径 方便存入数据库
            return '/bootcss/home/Images/user/'.$n.'.'.$suffix;
        }
    }
 }
